package com.cn.serverline.lineicp.productionLine.util;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.roses.kernel.dict.modular.entity.SysDict;
import cn.stylefeng.roses.kernel.dict.modular.pojo.request.DictRequest;
import cn.stylefeng.roses.kernel.dict.modular.service.DictService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Gmr
 * @since 2024/7/31
 */
@Component
public class DictUtil {

	// 字典服务
	@Resource(name = "dictServiceImpl")
	private DictService dictService;

	/**
	 * 根据字典类型和名称获取编码
	 *
	 * @param name 工步名
	 */
	public String getCodeByTypeAndName(String type,String name){
		DictRequest dictRequest = new DictRequest();
		dictRequest.setDictTypeCode(type);
		List<SysDict> workSectionTypeList = dictService.findList(dictRequest);
		if(CollectionUtil.isEmpty(workSectionTypeList)){
			return null;
		}
		Map<String, String> workSectionTypeMap = workSectionTypeList.stream()
				.collect(Collectors.toMap(SysDict::getDictName, SysDict::getDictCode));
		return workSectionTypeMap.get(name);
	}

	/**
	 * 校验字典是否存在
	 *
	 * @param type 字典类型
	 * @param code 字典编码
	 */
	public boolean validateDict(String type,String code){
		DictRequest dictRequest = new DictRequest();
		dictRequest.setDictTypeCode(type);
		List<SysDict> workSectionTypeList = dictService.findList(dictRequest);
		if(CollectionUtil.isEmpty(workSectionTypeList)){
			return false;
		}
		List<String> dictCodeList = workSectionTypeList.stream()
				.map(SysDict::getDictCode)
				.collect(Collectors.toList());
		return dictCodeList.contains(code);
	}
}
